﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Kefu
{
    public partial class CustomInfo : System.Web.UI.Page
    {
        public string alert;
        CustomDataContext db = new CustomDataContext();
        static DateTime begintime = Convert.ToDateTime("2000-1-1 0:0:0");
        static DateTime endtime = Convert.ToDateTime("2100-1-1 0:0:0");
        static Decimal MinMoney = 0;
        static Decimal MaxMoney = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var allProvince = from a in db.TCrm_Sys_Items where a.ClassID == "sf" select a;
                DropDownList_Province.Items.Add(new ListItem("--省份--", ""));
                DropDownList_Provinces.Items.Add(new ListItem("--省份--", ""));
                DropDownList_CustomerLevel.Items.Add(new ListItem("--客户等级--", ""));
                foreach (var query in allProvince)
                {
                    DropDownList_Province.Items.Add(new ListItem(query.ItemName.Trim(), query.ItemName.Trim()));
                    DropDownList_Provinces.Items.Add(new ListItem(query.ItemName.Trim(), query.ItemName.Trim()));

                }
                var allLevel = from a in db.TCrm_Sys_Items where a.ClassID == "khdj" select a;
                foreach (var query in allLevel)
                {
                    DropDownList_CustomerLevel.Items.Add(new ListItem(query.ItemName, query.ItemID));
                }

                EditFlag.Value = "add";
                //查询当前操作员的店铺权限及客户回访等级权限。
                string MyUserId = HttpContext.Current.Session[SystemVars.sysSessionUserId].ToString();
                var RightList = from a in db.TCsm_JS_User
                                where a.UserID == MyUserId
                                select new
                                {
                                    a.ShopRightID

                                };
                if (RightList.Count() > 0)
                {

                    string MyShopRightIdList = RightList.Single().ShopRightID;
                    if (!string.IsNullOrEmpty(MyShopRightIdList))
                    {
                        var DropList_ShopRight = MyShopRightIdList.Split(',');
                        foreach (var MyItems in DropList_ShopRight)
                        {
                            var MyShopName = from a in db.TCrm_JC_Parameter
                                              where a.ID == Convert.ToInt32(MyItems)
                                              select a;
                            if (MyShopName.Count() == 1)
                            {
                               var  AddItem = MyShopName.Single();
                                ListItem InsertItem = new ListItem(AddItem.ShopName, AddItem.ShopID);
                                //填充店铺列表
                                DropDownList_ShopId.Items.Add(InsertItem);
                                DropDownList_Shops.Items.Add(InsertItem);
                            }

                        }

                    }
                }
            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex, string starts)
        {
            if (string.IsNullOrEmpty(TextBox_Min.Text) == false)
            {
                if(PublicMethod.IsNumeric(TextBox_Min.Text)==true)
                {
                    MinMoney = Convert.ToDecimal(TextBox_Min.Text);
                }
            }
            if (string.IsNullOrEmpty(TextBox_Max.Text) == false)
            {
                if(PublicMethod.IsNumeric(TextBox_Min.Text)==true)
                {
                    MaxMoney = Convert.ToDecimal(TextBox_Max.Text);
                }
            }
            string begin = TextBox_BeginTime.Text;
            string end = TextBox_EndTime.Text;
            string Outid = TextBox_Product.Text;
            if (begin != "" || end != "")
            {
                try
                {
                    begintime = Convert.ToDateTime(begin);
                    endtime = Convert.ToDateTime(end);
                }
                catch
                {
                }
            }
            string KeySelect = DropDownList_Key.SelectedValue;
            string KeyWord = TextBox_Key.Text;
            int skipAccount = PageIndex * 10;
            string ShopId = DropDownList_ShopId.SelectedValue;
            string Provinces = DropDownList_Provinces.SelectedValue;
            var Customid = from c in db.VCrm_KF_Orders where (KeySelect == "Order"?c.tid == KeyWord:false) select c;
            var querya = from c in db.TCrm_KF_Orders
                         where (Outid == "" ? false : c.outerid==Outid)
                         select c.buyer_nick;
            string BuyerNick=Customid.Count()==1?Customid.Single().buyer_nick:"";
            var MyQuery = (from a in db.TCrm_KF_Customer
                           where (starts == "" ? true : a.CustomerID.StartsWith(starts))
                           && (ShopId.Trim() == "" ? true : a.ShopID.Trim() == ShopId.Trim())
                           && (KeySelect == "" ? true : (KeySelect == "Ali" ? a.AliPayID.StartsWith(KeyWord) : (KeySelect == "Phone" ? a.MobliePhone.StartsWith(KeyWord) : (KeySelect == "Receiver" ? a.ReceiverName.StartsWith(KeyWord) : (KeySelect == "Order" ? (BuyerNick == "" ? true : a.TbID.StartsWith(BuyerNick)): true)))))
                           //&& (Receive == "" ? true : a.ReceiverName.StartsWith(Receive))
                           && (MinMoney == Convert.ToDecimal(0) ? true : a.AllMoney > MinMoney)
                           && (MaxMoney == Convert.ToDecimal(0) ? true : a.AllMoney < MaxMoney)
                           //&& (Ali == "" ? true : a.AliPayID.StartsWith(Ali))
                           //&& (BuyerNick == "" ? true : a.TbID.StartsWith(BuyerNick))
                           && (Provinces == "" ? true : a.Province.StartsWith(Provinces))
                           //&& (Phone == "" ? true : a.MobliePhone.StartsWith(Phone))
                           && (begintime == Convert.ToDateTime("2000-1-1 0:0:0") ? true : a.FinalTime > begintime)
                           && (endtime == Convert.ToDateTime("2100-1-1 0:0:0") ? true : a.FinalTime < endtime)
                           && (querya.Count() == 0 ? true : querya.Contains(a.TbID))
                           select a).Skip(skipAccount).Take(10);
            GridView_Orders.DataSource = MyQuery;
            GridView_Orders.DataBind();
            if (MyQuery.Count() > 0)
            {
                Panel_Browse.Visible = true;
            }
            else
            {
                Panel_Browse.Visible = false;
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('没有符合条件的记录'); </script>");
            }

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount(string starts)
        {
            string begin = TextBox_BeginTime.Text;
            string end = TextBox_EndTime.Text;
            string KeySelect = DropDownList_Key.SelectedValue;
            string KeyWord = TextBox_Key.Text;
            string ShopId = DropDownList_ShopId.SelectedValue;
            string Outid = TextBox_Product.Text;
            var Customid = from c in db.VCrm_KF_Orders where (KeySelect == "Order" ? c.tid == KeyWord : true) select c;
            var querya = from c in db.TCrm_KF_Orders
                         where (Outid == "" ? true : c.outerid == Outid)
                         select c.buyer_nick;
            string Provinces = DropDownList_Provinces.SelectedValue;
            string BuyerNick = Customid.Count() == 1 ? Customid.Single().buyer_nick : "";
            int MyQuery = 0;
            MyQuery = (from a in db.TCrm_KF_Customer
                       where (starts == "" ? true : a.CustomerName.StartsWith(starts))
                       && (ShopId.Trim() == "" ? true : a.ShopID.Trim() == ShopId.Trim())
                       && (KeySelect == "" ? true : (KeySelect == "Ali" ? a.AliPayID.StartsWith(KeyWord) : (KeySelect == "Phone" ? a.MobliePhone.StartsWith(KeyWord) : (KeySelect == "Receiver" ? a.ReceiverName.StartsWith(KeyWord) : (KeySelect == "Order" ? (BuyerNick == "" ? true : a.TbID.StartsWith(BuyerNick)) : true)))))
                           //&& (Receive == "" ? true : a.ReceiverName.StartsWith(Receive))
                       && (MinMoney == Convert.ToDecimal(0) ? true : a.AllMoney > MinMoney)
                       && (MaxMoney == Convert.ToDecimal(0) ? true : a.AllMoney < MaxMoney)
                           //&& (Ali == "" ? true : a.AliPayID.StartsWith(Ali))
                           //&& (BuyerNick == "" ? true : a.TbID.StartsWith(BuyerNick))
                       && (Provinces == "" ? true : a.Province.StartsWith(Provinces))
                           //&& (Phone == "" ? true : a.MobliePhone.StartsWith(Phone))
                       && (begintime == Convert.ToDateTime("2000-1-1 0:0:0") ? true : a.FinalTime > begintime)
                       && (endtime == Convert.ToDateTime("2100-1-1 0:0:0") ? true : a.FinalTime < endtime)
                       && (querya.Count() == 0 ? true : querya.Contains(a.TbID))
                       select a).Count();
            return MyQuery;
        }
        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {
            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            string starts = txtOrder.Text;
            int Totals = TotalPageAmount(starts);
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx, starts);

        }

        #endregion
        //取消的按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            EditFlag.Value = "0";
            Panel_Edit.Visible = false;
            Panel_Gridlist.Visible = true;
            if (EditFlag.Value == "edit")
            {
                TextBox_CustomerID.Enabled = true;

            }

        }
        //增加按钮的事件
        protected void Button_Excel_Command(object sender, CommandEventArgs e)
        {
            Panel_Gridlist.Visible = false;
            Panel_Excel.Visible = true;
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('批量导入','Panel_Excel');</script>", false);
        }
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_Gridlist.Visible = false;
            TextBox_CustomerID.Text = "";
            TextBox_Address.Text = "";
            TextBox_Belongto.Text = "";
            TextBox_CallBackLevel.Text = "";
            TextBox_CustomerName.Text = "";
            TextBox_Email.Text = "";
            TextBox_TbID.Text = "";
            DropDownList_Province.SelectedValue = "";
            DropDownList_CustomerLevel.SelectedValue = "";
            TextBox_CustomerID.Enabled = false;
            Panel_Edit.Visible = true;
            EditFlag.Value = "add";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('增加','Panel_Edit');</script>", false);
        }
        //保存的按钮
        protected void Button_UploadExcel_Command(object sender, CommandEventArgs e)
        {
            string timenow = DateTime.Now.ToString("yyyyMMddhhmmss");
            string MyFileName = "";
            if (FileUpload1.HasFile)
            {
                string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                string[] allowedExtensions = { ".xls" };
                if (fileExtension == allowedExtensions[0])
                {
                    MyFileName = HttpContext.Current.Server.MapPath(@"excelfile\") + FileUpload1.FileName;

                    //判断当前路径是否已存在，如果存在的话，就先删除掉。
                    if (File.Exists(MyFileName))
                    {
                        File.Delete(MyFileName);
                    }

                    try
                    {
                        FileUpload1.SaveAs(MyFileName);
                        InputExcel(MyFileName, FileUpload1.FileName.Replace(".xls",""));
                        System.IO.File.Delete(MyFileName);
                        Response.AddHeader("Refresh", "0");
                    }
                    catch(Exception ex)
                    {
                        alert = "<script>alert('上传失败"+ex.Message.ToString()+"');</script>";
                    }

                }
                else
                {
                    alert = "<script>alert('请检查您选择的文件是否为Excel文件！谢谢！');</script>";
                }
            }
            else
            {

                alert = "<script>alert('请先选择导入文件后，再执行导入！谢谢！！');</script>";

            }
        }
        //EXCEL数据写入代码。
        private void InputExcel(string pPath,string pName)
        {
            string conn = "Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source =" + pPath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
            OleDbConnection oleCon = new OleDbConnection(conn);
            oleCon.Open();
            string Sql = "select * from ["+pName+"$]";
            OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, oleCon);
            DataSet ds = new DataSet();
            mycommand.Fill(ds);
            oleCon.Close();
            int count = ds.Tables[0].Rows.Count;
            int countsimilar = 0;
            string addshop = "";
            for (int i = 1; i < count; i++)
            {
                string number = ds.Tables[0].Rows[i][1].ToString();
                var querysame = from a in db.TCrm_KF_Customer where a.CustomerID == number select a;
                if (querysame.Count() == 0)
                {
                    string shopid = ds.Tables[0].Rows[i][30].ToString();
                    var queryshop=from a in db.TCrm_JC_Parameter where a.ShopID==shopid select a;
                    if (queryshop.Count() == 0)
                    {
                        if (addshop.Split(',').Contains(shopid) == false)
                        {
                            addshop += shopid + ",";
                        }
                    }
                    else
                    {
                        countsimilar++;
                        TCrm_KF_Customer RowData = new TCrm_KF_Customer();
                        RowData.CustomerID = ds.Tables[0].Rows[i][1].ToString();
                        RowData.CustomerName = ds.Tables[0].Rows[i][2].ToString();
                        RowData.Sex = ds.Tables[0].Rows[i][3].ToString();
                        RowData.Province = ds.Tables[0].Rows[i][4].ToString();
                        RowData.TbID = ds.Tables[0].Rows[i][5].ToString();
                        RowData.MSN = ds.Tables[0].Rows[i][6].ToString();
                        RowData.AliPayID = ds.Tables[0].Rows[i][7].ToString();
                        RowData.CallNum = ds.Tables[0].Rows[i][8].ToString();
                        RowData.MobliePhone = ds.Tables[0].Rows[i][9].ToString();
                        RowData.Fax = ds.Tables[0].Rows[i][10].ToString();
                        RowData.Postal = ds.Tables[0].Rows[i][11].ToString();
                        RowData.Email = ds.Tables[0].Rows[i][12].ToString();
                        RowData.ReceiverName = ds.Tables[0].Rows[i][13].ToString();
                        RowData.Address = ds.Tables[0].Rows[i][14].ToString();
                        RowData.IDType = ds.Tables[0].Rows[i][15].ToString();
                        RowData.IDNumber = ds.Tables[0].Rows[i][16].ToString();
                        RowData.ComputerMature = ds.Tables[0].Rows[i][17].ToString();
                        RowData.Birthday = ds.Tables[0].Rows[i][18].ToString();
                        if (ds.Tables[0].Rows[i][19].ToString() == "")
                        {
                            RowData.DevelopTime = null;
                        }
                        else
                        {
                            RowData.DevelopTime = Convert.ToDateTime(ds.Tables[0].Rows[i][19]);
                        }
                        RowData.Note = ds.Tables[0].Rows[i][20].ToString();
                        RowData.Belongto = ds.Tables[0].Rows[i][21].ToString();
                        RowData.Inputer = ds.Tables[0].Rows[i][22].ToString();
                        RowData.Credit = Convert.ToDouble(ds.Tables[0].Rows[i][23].ToString());
                        if (ds.Tables[0].Rows[i][24].ToString() == "")
                        {
                            RowData.UsedCredit = null;
                        }
                        else
                        {
                            RowData.UsedCredit = Convert.ToDouble(ds.Tables[0].Rows[i][24].ToString());
                        }
                        if (ds.Tables[0].Rows[i][25].ToString() == "")
                        {
                            RowData.ComsumeMoney = null;
                        }
                        else
                        {
                            RowData.ComsumeMoney = Convert.ToDouble(ds.Tables[0].Rows[i][25].ToString());
                        }
                        if (ds.Tables[0].Rows[i][26].ToString() == "")
                        {
                            RowData.AllMoney = null;
                        }
                        else
                        {
                            RowData.AllMoney = Convert.ToDecimal(ds.Tables[0].Rows[i][26].ToString());
                        }
                        RowData.Num = ds.Tables[0].Rows[i][27].ToString();
                        RowData.Frequence = ds.Tables[0].Rows[i][28].ToString();
                        if (ds.Tables[0].Rows[i][29].ToString() == "")
                        {
                            RowData.FinalTime = null;
                        }
                        else
                        {
                            RowData.FinalTime = Convert.ToDateTime(ds.Tables[0].Rows[i][29]);
                        }
                        RowData.ShopID = ds.Tables[0].Rows[i][30].ToString();
                        if (ds.Tables[0].Rows[i][31].ToString() == "")
                        {
                            RowData.NoCall = null;
                        }
                        else
                        {
                            RowData.BlackList = Convert.ToBoolean(ds.Tables[0].Rows[i][31]);
                        }
                        db.TCrm_KF_Customer.InsertOnSubmit(RowData);
                        db.SubmitChanges();
                    }
                }
                else
                {
                }
            }
            int diff = count-1 - countsimilar;
            if (Convert.ToInt32(diff) == 0)
            {
            }
            else
            {
                if (addshop == "")
                {
                    alert = ("<script>alert('排除" + diff + "个相同客户编码成功添加" + countsimilar + "条客户信息');</script>");
                }
                else
                {
                    alert = ("<script>alert('排除" + diff + "个相同客户编码成功添加" + countsimilar + "条客户信息,请在系统中添加"+addshop+"店铺后再上传');</script>");
                }
            }
        }
        protected void Button_ExitExcel_Command(object sender, CommandEventArgs e)
        {
            Panel_Excel.Visible = false;
            Panel_Gridlist.Visible = true;
        }
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {

            if (EditFlag.Value == "add")
            {


                TCrm_KF_Customer myrow = new TCrm_KF_Customer();
                myrow.CustomerID=PublicMethod.CreateCustomer();
                myrow.ShopID = DropDownList_Shops.SelectedValue;
                myrow.Address=TextBox_Address.Text;
                myrow.Belongto=TextBox_Belongto.Text;
                myrow.CallBackLevel=TextBox_CallBackLevel.Text;
                myrow.MobliePhone = TextBox_Phone.Text;
                myrow.CustomerName=TextBox_CustomerName.Text;
                myrow.Email=TextBox_Email.Text;
                myrow.TbID=TextBox_TbID.Text;
                myrow.Province=DropDownList_Province.SelectedValue ;
                myrow.CustomerLevel=DropDownList_CustomerLevel.SelectedValue ;
                db.TCrm_KF_Customer.InsertOnSubmit(myrow);
                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    BindMyData(0, "");
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }


            }
            else if (EditFlag.Value == "edit")
            {
                string myUserId = TextBox_CustomerID.Text;
                var myEditQuery = (from a in db.TCrm_KF_Customer where a.CustomerID == myUserId select a).Single();
                myEditQuery.CustomerID = TextBox_CustomerID.Text;
                myEditQuery.MobliePhone = TextBox_Phone.Text;
                myEditQuery.ShopID = DropDownList_Shops.SelectedValue;
                myEditQuery.Address = TextBox_Address.Text;
                myEditQuery.AliPayID = TextBox_Belongto.Text;
                myEditQuery.CallBackLevel = TextBox_CallBackLevel.Text;
                myEditQuery.CustomerName = TextBox_CustomerName.Text;
                myEditQuery.Email = TextBox_Email.Text;
                myEditQuery.TbID = TextBox_TbID.Text;
                myEditQuery.Province = DropDownList_Province.SelectedValue;
                myEditQuery.CustomerLevel = DropDownList_CustomerLevel.SelectedValue;

                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    TextBox_CustomerID.Enabled = true;
                    BindMyData(0, "");

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑失败');</script>");
                }

            }

        }
        protected void Button_Change_Command(object sender, CommandEventArgs e)
        {
            string myUserId = e.CommandArgument.ToString().Trim();
            var myEditQuery = (from a in db.TCrm_KF_Customer
                               where a.CustomerID == myUserId
                               select a).Single();
            TextBox_CustomerID.Text = myEditQuery.CustomerID;
            TextBox_Address.Text = myEditQuery.Address;
            TextBox_Belongto.Text = myEditQuery.AliPayID;
            TextBox_CallBackLevel.Text = myEditQuery.CallBackLevel;
            DropDownList_Shops.SelectedValue = myEditQuery.ShopID;
            TextBox_Phone.Text = myEditQuery.MobliePhone;
            TextBox_CustomerName.Text = myEditQuery.CustomerName;
            TextBox_Email.Text = myEditQuery.Email;
            TextBox_TbID.Text = myEditQuery.TbID;
            DropDownList_CustomerLevel.SelectedValue = myEditQuery.CustomerLevel;
            DropDownList_Province.SelectedValue = myEditQuery.Province;
            TextBox_CustomerID.Enabled = false;
            Panel_Edit.Visible = true;
            Panel_Gridlist.Visible = false;
            EditFlag.Value = "edit";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('编辑','Panel_Edit');</script>", false);

        }
        protected void Button_Browse_Click(object sender, EventArgs e)
        {
            string user = txtOrder.Text;
            BindMyData(0, user);
            int Totals = TotalPageAmount(user);
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
        }
        public string GetSubString(string stringToSub)
        {
            int length = 30;
            if (stringToSub.Length != 0)
            {
                if (stringToSub.Length <= length)
                    return stringToSub;
                else
                {
                    return stringToSub.Substring(0, length) + "...";
                }
            }

            else
            {
                return "";
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var query = from a in db.TCrm_KF_Customer select a;
            foreach (var a in query)
            {
                a.Province = PublicMethod.GetProvince(a.Address);
                db.SubmitChanges();
            }
        }


    }
}
